import React from "react";
import { Button, Form, Input,message } from "antd";
import { UserOutlined, LockOutlined } from "@ant-design/icons";
import { Link,useNavigate } from "react-router-dom";
import {LoginApi} from '../request/api'
import "./less/Login.less";
export default function Login() {
  const navigate=useNavigate()
  const onFinish = (values) => {
    // console.log("Success:", values);
    LoginApi({
      username:values.username,
      password:values.password
    }).then(res=>{
      console.log(res);
      if(res.errCode===0){
        message.success('登陆成功');
        //存储数据
        localStorage.setItem('avatar',res.data.avatar)
        localStorage.setItem('editable',res.data.editable)
        localStorage.setItem('player',res.data.player)
        localStorage.setItem('username',res.data.username)
        localStorage.setItem('cms-token',res.data['cms-token'])
        setTimeout(() => {
          navigate('/')
        }, 1500);
      }else{
        message.error(res.message);
      }
    })
  };

  const onFinishFailed = (errorInfo) => {
    console.log("Failed:", errorInfo);
  };
  return (
    <div className="login">
      <div className="login_box">
        <Form
          name="basic"
          initialValues={{
            remember: true,
          }}
          onFinish={onFinish}
          onFinishFailed={onFinishFailed}
          autoComplete="off"
        >
          <Form.Item
            label="Username"
            name="username"
            rules={[
              {
                required: true,
                message: "Please input your username!",
              },
            ]}
          >
            <Input
              prefix={<UserOutlined />}
              placeholder="Please input your username!"
            />
          </Form.Item>

          <Form.Item
            label="Password"
            name="password"
            rules={[
              {
                required: true,
                message: "Please input your password!",
              },
            ]}
          >
            <Input.Password
              prefix={<LockOutlined />}
              placeholder="Please input your password!"
            />
          </Form.Item>

          <Form.Item
            wrapperCol={{
              offset: 2,
              span: 16,
            }}
          >
            <Link to="/register" className="ToRegister">
              I have not account,to Reginster!
            </Link>
          </Form.Item>

          <Form.Item
            wrapperCol={{
              offset: 8,
              span: 16,
            }}
          >
            <Button type="primary" htmlType="submit">
           Login
            </Button>
          </Form.Item>
        </Form>
      </div>
    </div>
  );
}
